<?php
/**
 * Plugin para gerenciar includes nas controllers
 * @author felipedasilva
 *
 */
class AutenticacaoController extends Zend_Controller_Action
{       
        
        public function indexAction(){
        	$this->_helper->layout->setLayout('login');
            $this->view->form = new App_Form_Login();
        }
        
        public function logarAction(){
            $this->_helper->viewRenderer->setNoRender();
            if(!$this->getRequest()->isPost()){
                $this->_forward('index');
            }
            $obForm = new App_Form_Login();
            if($obForm->isValid($this->getRequest()->getPost())){                
                $obServicoAutenticacao = new Voitech_Servico_Autenticacao();
                if( !$obServicoAutenticacao->autenticar($this->_getParam('no_usuario'), $this->_getParam('ds_senha')) ){
                    $this->_helper->FlashMessenger->addMessage('Dados Inválidos');
                    $this->_redirect('autenticacao/'); 
                }
                $this->_redirect('index');
            }
            else
            {
                $this->_helper->FlashMessenger->addMessage('Dados Inválidos');
                $this->_redirect('autenticacao/'); 
            }
        }        
        
        /**
         * Metodo para deslogar no sistema
         */
        public function logofAction()
        {  
           $this->_helper->layout->disableLayout();		
           $this->_helper->viewRenderer->setNoRender(); 
           $obAuth = Zend_Auth::getInstance();
           $obAuth->clearIdentity();              
           $this->_redirect('autenticacao/');
        }
        
        public function callbackTwitterAction(){
           $this->_helper->layout->disableLayout();		
           $this->_helper->viewRenderer->setNoRender();
           try{
               $consumer = new Zend_Oauth_Consumer(Voitech_Config::TWITTER);

                if (!empty($_GET) && isset($_SESSION['TWITTER_REQUEST_TOKEN']))
                {
                    $token = $consumer->getAccessToken($_GET, unserialize($_SESSION['TWITTER_REQUEST_TOKEN']));

                    // Now that we have an Access Token, we can discard the Request Token
                    $_SESSION['TWITTER_REQUEST_TOKEN'] = null;
                } 
                 else 
                {
                    // Mistaken request? Some malfeasant trying something?
                    exit('Invalid callback request. Oops. Sorry.');
                }

                $client = $token->getHttpClient(Voitech_Config::TWITTER);
                $client->setUri('http://twitter.com/statuses/update.json');
                $client->setMethod(Zend_Http_Client::POST);
                $client->setParameterPost('status', 'twitei nessa bosta');
                $response = $client->request();

                $data = json_decode($response->getBody());
                echo $data->text;
           }catch(Exception $e){
               echo $e->getMessage();
           }
        }
		
}